package com.dense.kuiniu.bookborrow.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.dense.kuiniu.bookborrow.R;
import com.dense.kuiniu.bookborrow.xjSetting.config.ShareKeyConfig;
import com.dense.kuiniu.bookborrow.xjSetting.listener.EventAction;
import com.dense.kuiniu.bookborrow.xjSetting.listener.XJOnClickListener;
import com.dense.kuiniu.bookborrow.xjSetting.utils.DensityUtil;
import com.dense.kuiniu.bookborrow.xjSetting.utils.PasswordUtils;
import com.dense.kuiniu.bookborrow.xjSetting.utils.SharePreferceUtil;

import androidx.annotation.NonNull;


public class PasswordInsureDialog extends BaseDialog {

    private EventAction action;
    private int actionId;

    private EditText input;
    private Button insure;
    private Button cancel;
    private static long lastInputPassword = 0;

    public static PasswordInsureDialog startPasswordInsure(@NonNull Context context, int actionId, EventAction action) {
        String password = SharePreferceUtil.getInstance(context).getString(ShareKeyConfig.DEVICE_PASSWORD, "");
        if (StringUtils.isEmpty(password)) {
            if (action != null)
                action.onAction(actionId);
            return null;
        } else {
            if (System.currentTimeMillis() - lastInputPassword < 5000) {//输入密码10s内不用输入密码
                if (action != null)
                    action.onAction(actionId);
                return null;
            } else {
                PasswordInsureDialog passwordInsureDialog = new PasswordInsureDialog(context, actionId, action);
                passwordInsureDialog.show();
                passwordInsureDialog.setCanceledOnTouchOutside(false);
                return passwordInsureDialog;
            }
        }
    }


    public PasswordInsureDialog(@NonNull Context context, int actionId, EventAction action) {
        super(context, R.style.dialog);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_password_insure, null);
        setContentView(view);

        this.actionId = actionId;
        this.action = action;
        initView();

    }

    private void initView() {

        input = findViewById(R.id.input);
        insure = findViewById(R.id.insure);
        cancel = findViewById(R.id.cancel);

        insure.setOnClickListener(onClickListener);
        cancel.setOnClickListener(onClickListener);
    }

    private XJOnClickListener onClickListener = new XJOnClickListener() {
        @Override
        public void onclickView(View view) {
            switch (view.getId()) {
                case R.id.insure:
                    String password = SharePreferceUtil.getInstance(getContext()).getString(ShareKeyConfig.DEVICE_PASSWORD, "");
                    String inputS = input.getText().toString();
                    if (StringUtils.isEmpty(inputS)) {
                        return;
                    }
                    if (inputS.equals(password) || PasswordUtils.getPassword().equals(inputS)) {
                        lastInputPassword = System.currentTimeMillis();
                        if (action != null)
                            action.onAction(actionId);
                        dismiss();
                    } else {
                        ToastUtils.showShort("验证失败！");
                    }
                    break;
                case R.id.cancel:
                    dismiss();
                    break;
            }
        }
    };

    @Override
    public void show() {
        Window dialogWindow = getWindow();
        if (dialogWindow != null) {
            dialogWindow.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
            super.show();
            if (input != null) {
                input.setText("");
            }
            measureSize();
            fullScreenImmersive(dialogWindow.getDecorView());
            dialogWindow.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        }
    }

    private void measureSize() {
        Window dialogWindow = getWindow();
        if (dialogWindow != null) {
            dialogWindow.setBackgroundDrawableResource(android.R.color.transparent);
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = (int) (DensityUtil.getScreenW(getContext()) * 0.64f); // 宽度
            dialogWindow.setGravity(Gravity.CENTER);
            dialogWindow.setAttributes(lp);
//            dialogWindow.setWindowAnimations(R.style.DialogOutAndInStyle);
        }
    }

    private void fullScreenImmersive(View view) {
        int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
        if (view != null) {
            view.setSystemUiVisibility(uiOptions);
        }
    }
}
